<?php

class BPlugin_request extends BPlugin
{
	/**
	 * 取get值
	 *
	 */
	const GET       = 1;

	/**
	 * 取post值
	 *
	 */
	const POST      = 2;

    /**
     * 取上传值
     *
     */
    const FILES     = 3;
    
	/**
	 * 取得GET跟POST
	 */
	const REQUEST   = 4; 

	/**
	 * 所有请求数据
	 *
	 * @access private
	 * @var array
	 */
	private $_request = array();

	/**
	 * get请求数据
	 *
	 * @access private
	 * @var array
	 */
	private $_get = array();

	/**
	 * post请求数据
	 *
	 * @access private
	 * @var array
	 */
	private $_post = array();

    /**
     * 文件上传数据
     *
     * @access private
     * @var array
     */
	private $_files = array();

	/**
	 * 取数据标识，如为空则从request中取
	 *
	 * @access private
	 * @var string
	 */
	private $_method = '';

	/**
	 * 构造器
	 *
	 * @access public
	 * @return null
	 */
	function __construct()
	{
        $formatget = $this->urlformat($this->urlformat($_SERVER['REQUEST_URI'], 'out'), 'in');
		$this->_get = $formatget[B::i()->config->action] ? array_merge($_GET, $formatget) : $_GET;

		$this->_post = $_POST;
		$this->_request = array_merge($this->_get, $this->_post);
		$this->_files = $_FILES;
		//$_GET = $_POST = $_REQUEST = $_FILES = array();
	}

	/**
	 * 当前是否是post请求
	 *
	 * @access public
	 * @return bool
	 */
	function isPost($name = null)
	{
        $ispost = "POST" == strtoupper($_SERVER["REQUEST_METHOD"]);
        if ($ispost && $name) {
            return isset($this->_post[$name]) && !empty($this->_post[$name]);
        }
        return $ispost;
	}

    /**
     * 当前请求是否ajax请求
     *
     * @access public
     * @return bool
     */
    function isAjax()
    {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }

	/**
	 * 当前请求uri
	 *
	 * @access public
	 * @return string
	 */
	function Uri()
	{
		return $_SERVER['PHP_SELF'];
	    //return $_SERVER['REQUEST_URI'];
	}

    /**
     * 获取引用地址
     *
     * @access public
     * @return mixed
     */
    static function Referer()
    {
    	return $_SERVER['HTTP_REFERER'];
    }

	/**
	 * 获取来访者IP地址
	 *
	 * @access public
	 * @param bool $convert
	 * @return string
	 */
	function ip($convert = false)
	{
        static $ip;
        if (is_null($ip)){
            isset($_SERVER['HTTP_CLIENT_IP']) && $ips[] = $_SERVER['HTTP_CLIENT_IP'];
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
	            foreach (explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']) as $ip){
	                $ips[] = $ip;
	            }
            }
            isset($_SERVER['HTTP_X_FORWARDED']) && $ips[] = $_SERVER['HTTP_X_FORWARDED'];
            isset($_SERVER['HTTP_FORWARDED_FOR']) && $ips[] = $_SERVER['HTTP_FORWARDED_FOR'];
            isset($_SERVER['HTTP_FORWARDED']) && $ips[] = $_SERVER['HTTP_FORWARDED'];
            isset($_SERVER['REMOTE_ADDR']) && $ips[] = $_SERVER['REMOTE_ADDR'];
            $find = false;
            $ips = array_filter($ips);
            while ($find || !$ip = array_shift($ips)){
                $find = $ip != ''
                    && false !== ip2long($ip);
            }
        }
        return $convert ? sprintf('%u', ip2long($ip)) : $ip;
	}

	/**
	 * 设置取值方法
	 *
	 * @access public
	 * @param string $method
	 * @return mixed
	 */
	function method($method)
	{
        $flags = array(
            self::GET,
            self::POST,
            self::FILES,
        );
        if (in_array($method, $flags)) {
        	$this->_method = $method;
        	return $this;
        }
        throw new DException('params_unsupported', $method);
	}

	/**
	 * 取请求名对应的值
	 *
	 * @access public
	 * @param string $name
	 * @return mixed
	 */
	function data($name = null)
	{
		$val = '';
		if (is_null($name)) {
			if ($this->_method == self::GET) {
				$val = $this->_get;
			} elseif ($this->_method == self::POST) {
				$val = $this->_post;
            } elseif ($this->_method == self::FILES) {
                $val = $this->_files;
			} elseif ($this->_method == self::REQUEST) {
                $val = $this->_request;
			} else {
				$val = $this->_request;
			}
		} else {
	        if ($this->_method == self::GET) {
	            $val = str_replace("\r\n", PHP_EOL, $this->filterSign($this->_get[$name]));
	        } elseif ($this->_method == self::POST) {
	            $val = str_replace("\r\n", PHP_EOL, $this->filterSign($this->_post[$name]));
            } elseif ($this->_method == self::FILES) {
                $val = $this->_files[$name];
	        } elseif ($this->_method == self::REQUEST) {
                $val = $this->filterSign($this->_request[$name]);
			} else {
	            $val = $this->filterSign($this->_request[$name]);
	        }
		}
		return $val;
	}

	/**
	 * 重置当前取值方法
	 *
	 * @access public
	 * @return object
	 */
	function reset()
	{
        $this->_method = '';
        $_GET = $_POST = $_REQUEST = $_FILES = array();
        return $this;
	}

    /**
     * 添加新数据到request中
     *
     * @access public
     * @param array $data
     * @param int $flag
     * @return bool
     */
    function add($data, $flag = self::GET)
    {
        if (!is_array($data)) {
            throw new DException('data_require_an_array');
        }
        switch($flag) {
            case self::GET:
                $this->_get = array_merge($this->_get, $data);
                break;
            case self::POST:
                $this->_post = array_merge($this->_post, $data);
                break;
        }
        $this->_request = array_merge($this->_get, $this->_post);
        return $this;
    }

    /**
     * 过滤
     *
     * @access public
     * @param array $data
     * @return string
     */
	function filterSign ($data)
    {
        return $data;
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->filterSign($v);
            }
        } elseif (is_string($data)) {
            $data = str_replace(array('>', '<'), array('&gt;', '&lt;'), strip_tags($data));
        }
        return $data;
    }

    /**
     * url格式化
     *
     * @access public
     * @param string $uri
     * @param  string $type 类型 out:输出 in:输入
     * @return string
     */
    public function urlformat($uri, $type = 'out')
    {
        //$action = $this->data(B::i()->config->action);
        if ($type == 'out') {
            $uri = str_replace('?', '', $uri);
    	    return str_replace('&', '/', $uri);
        } else {
            $relust = array();
            foreach (array_filter(explode('/', $uri)) as $u) {
                list($k_, $u_) = explode('=',$u);
                $result[$k_] = urldecode($u_);
            }
            return $result;
        }
    }

	/**
	 * 取数据
	 *
	 * @access public
	 * @param string $name
	 * @return mixed
	 */
	function __get($name)
	{
		if ($name == 'get') {
			$this->_method = self::GET;
			return $this;
		} elseif ($name == 'post') {
			$this->_method = self::POST;
			return $this;
		} elseif ($name == 'files') {
            $this->_method = self::FILES;
			return $this;
		} elseif ($name == 'request') {
            $this->_method = self::REQUEST;
			return $this;
		}
		return $this->data($name);
	}



}

?>
